package com.mxpio.erp.inventory.service.impl;

import com.mxpio.erp.common.enums.OrderEnums;
import com.mxpio.erp.common.inventory.entity.BorrowApplyLine;
import com.mxpio.erp.common.inventory.entity.InvFlow;
import com.mxpio.erp.common.inventory.entity.Lot;
import com.mxpio.erp.common.inventory.service.BorrowApplyLineService;
import com.mxpio.erp.common.technology.entity.Item;
import com.mxpio.erp.common.vo.InvFlowSummaryVo;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.lin.Linq;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BorrowApplyLineServiceImpl extends BaseServiceImpl<BorrowApplyLine> implements BorrowApplyLineService {
    @Override
    @Transactional(readOnly = true)
    public void handleBorrowApplyLinesReturnedQuantity(List<BorrowApplyLine> borrowApplyLines) {
        if (borrowApplyLines!=null&&borrowApplyLines.size()>0){//查询借用单已经归还数量
            for (BorrowApplyLine line:borrowApplyLines){
                List<InvFlow> returnedFlow = JpaUtil.linq(InvFlow.class)
                        .exists(Lot.class)
                        .equalProperty("lotNo", "lotNo")
                        .equalProperty("subLotIndex", "subLotIndex")
                        .isNotNull("originSubLotIndex")
                        .end()
                        .equal("accessType", OrderEnums.AccessType.OUT.getCode())//出
                        .equal("originBizNo", line.getBizNo())
                        .equal("originBizLineNo", line.getLineNo())
                        .equal("whCode", line.getWhCode()).list();
                BigDecimal completeQuantity = BigDecimal.ZERO;
                if (returnedFlow.size()>0){
                    completeQuantity = returnedFlow.stream().map(InvFlow::getQuantity).reduce(BigDecimal.ZERO, BigDecimal::add);
                }
                line.setActualQuantity(completeQuantity);
            }
        }
    }

    @Override
    @Transactional(readOnly = true)
    public Collection<InvFlowSummaryVo> getExecutedReturnInvFlows(String bizNo, String lineNo, String accessType, String whCode){
        Linq lin = JpaUtil.linq(InvFlow.class)
                .exists(Lot.class)
                    .equalProperty("lotNo", "lotNo")
                    .equalProperty("subLotIndex", "subLotIndex")
                    .isNotNull("originSubLotIndex")
                .end()
                .collect("itemCode", Item.class, "itemCode")
                .equal("originBizNo", bizNo)
                .equal("originBizLineNo", lineNo)
                .equal("whCode", whCode);
        //增加排序字段
        Criteria criteria = Criteria.create();
        criteria.addOrder(new com.mxpioframework.jpa.query.Order("lotNo",false))
                .addOrder(new com.mxpioframework.jpa.query.Order("subLotIndex",false));
        List<InvFlow> flows = lin.where(criteria).list();
        Map<String, InvFlowSummaryVo> summaryMap = new HashMap<>();

        for(InvFlow flow : flows){
            String key = flow.getLotNo()+flow.getSubLotIndex()+flow.getWhCode();
            InvFlowSummaryVo vo = summaryMap.get(key);

            if(vo == null){
                vo = new InvFlowSummaryVo();
                vo.setItemCode(flow.getItemCode());
                if(flow.getItem() != null){
                    vo.setItemName(flow.getItem().getItemName());
                    vo.setItemSpec(flow.getItem().getItemSpec());
                    vo.setDrawingNo(flow.getItem().getDrawingNo());
                    vo.setUnitCode(flow.getItem().getUnitCode());
                }
                vo.setLotNo(flow.getLotNo());
                vo.setSubLotIndex(flow.getSubLotIndex());
                vo.setWhCode(whCode);
                vo.setQuantity(BigDecimal.ZERO);
            }
            if(accessType.equals(flow.getAccessType())){
                vo.setQuantity(vo.getQuantity().add(flow.getQuantity()));
            }else{
                vo.setQuantity(vo.getQuantity().subtract(flow.getQuantity()));
            }
            if(BigDecimal.ZERO.compareTo(vo.getQuantity()) == 0){
                summaryMap.remove(key);
            }else{
                summaryMap.put(key, vo);
            }
        }
        for (String key:summaryMap.keySet()){
            InvFlowSummaryVo vo = summaryMap.get(key);
            if(BigDecimal.ZERO.compareTo(vo.getQuantity()) == 0){
                summaryMap.remove(key);
            }
        }
        return summaryMap.values();
    }

}
